/**
 * 
 */
package com.health.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author gaowen
 * @createDate 2013-3-13
 * @version 1.0
 * @tags 
 */
public class DB {
	/**
	 * get connection
	 * */
	public static Connection getCon() {
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost/health_manager?user=root&password=root";
			con = DriverManager.getConnection(url);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	
	/**
	 * get statement
	 * */
	public static Statement getSta(Connection con) {
		Statement sta = null;
		try {
			sta = con.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return sta;
	}
	
	/**
	 * get preparedStatement
	 * */
	public static PreparedStatement getPreSta(Connection con , String sql) {
		PreparedStatement preSta = null;
		try {
			preSta = con.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return preSta;
	}

	
	/**
	 * get resultSet
	 * */
	public static ResultSet getRs(Statement sta , String sql) {
		ResultSet rs = null;
		try {
			rs = sta.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	public static ResultSet getRs(PreparedStatement preSta) {
		ResultSet rs = null;
		try {
			rs = preSta.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	/**
	 * executeUpdate
	 * */
	
	
	/**
	 * close
	 * */
	public static void close(Connection con) {
		if(con != null) {
			try {
				con.close();
				con = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public static void close(Statement sta) {
		if(sta != null) {
			try {
				sta.close();
				sta = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public static void close(ResultSet rs){
		if(rs != null) {
			try {
				rs.close();
				rs = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	




}
